<?php
namespace
{
	
// 公共函数写这里，例如检查函数、过滤函数、权限检查函数
// 在自动化输入检查之后执行
// 可以直接使用input里边的变量


			$GLOBALS['lpconfig']['database']['dsn'] = 'mysql:host=localhost;port=3306;dbname=lyrest;charset=UTF8';
		$GLOBALS['lpconfig']['database']['user'] = 'root';
		$GLOBALS['lpconfig']['database']['password'] = 'root';
	}

namespace Lazyphp\Controller
{
	class LazyRestController extends AuthedController
	{
		public function __construct()
	    {
	            
	        if( function_exists('lazyrest_auth_check') )
	        {
	        	if( !in_array( g('a') , $noauth ) )
	        	{
					$this->auth();
					lazyrest_auth_check();
	        	}
	        }
	    }

	    		/**
* 向数据表user添加记录
* @ApiDescription(section="user", description="账号注册")
* @ApiLazyRoute(uri="/account/reg",method="GET|POST")
* @ApiParams(name="password", type="string",nullable=false,description="密码", check="check_not_empty", cnname="密码")
* @ApiParams(name="name", type="string",nullable=false,description="用户名", check="check_not_empty", cnname="用户名")
* @ApiParams(name="email", type="string",nullable=false,description="邮箱", check="check_not_empty", cnname="邮箱")
* @ApiReturn(type="object", sample="{'code': 0,'message': 'success'}")
*/
 function user_lcadd_1476426905644( $password,$name,$email )
{
	// 输入函数过滤
							
	// 输入控制代码
	
// 在自动化输入检查之后执行
// 可以直接使用input里边的变量
$password = sha1($password);


	// 检查唯一
		if( get_var("SELECT COUNT(*) FROM `user` WHERE `email` = '" . s( $email ) . "'") > 0 )
	{
		return send_error('INPUT' , '邮箱字段不能重复');
	}
			 	
	

	// 拼接SQL
		
						$pre_field[] = '`password`';
		$post_field[] = "'" . s( $password ) . "'";
				
				
		
						$pre_field[] = '`name`';
		$post_field[] = "'" . s( $name ) . "'";
				
				
		
						$pre_field[] = '`email`';
		$post_field[] = "'" . s( $email ) . "'";
				
				
	
	// 业务逻辑控制代码
	
// 在$sql拼接之前，可以对其组成数组进行修改
// 在UPDATE、DELETE操作中，可使用$last对原始记录进行检查


	$sql = "INSERT INTO `user` ( " . join( ' , ' , $pre_field ) . " ) VALUES ( " . join( ' , ' , $post_field ) . " )  ";

	run_sql( $sql );
	$theid = last_id();

			$out_field[] = '`id`';
				$out_field[] = '`email`';
				$out_field[] = '`name`';
				$out_field[] = '`password`';
				$out_field[] = '`level`';
			

	$return_item = get_line("SELECT " . join( ' , ' , $out_field ) . " FROM `user` WHERE `id` = '" . intval( $theid ) . "' LIMIT 1");

	// 输出控制代码
	
// 在return之前运行，可以对$return_item 进行修改

	if( $return_item )
	{
		return render_json( $return_item );	
	} 
	else
	{
		return send_error('DATABASE' , '数据插入失败');
	};


}

	    
	    
		
	    		/**
* 查询数据表user的记录
* @ApiDescription(section="user", description="测试")
* @ApiLazyRoute(uri="/account/ceshi",method="GET|POST")
* @ApiParams(name="name", type="string",nullable=false,check="check_not_empty", ,description="用户名", cnname="用户名")
* @ApiReturn(type="object", sample="{'code': 0,'message': 'success'}")
*/
 function user_lclist_1476672751598( $name )
{
	
	$page_count = 10;
	$where_logic = ' AND ';

	// 输入过滤
		
			if( isset( $_REQUEST['name'] ) && !check_not_empty( $name ) )
	{
		return send_error( 'INPUT' , '字段用户名(name)格式错误' );
	}
	
		
	
	// 输入控制代码
	
// 在自动化输入检查之后执行
// 可以直接使用input里边的变量


	
	// 拼接SQL准备

			if( strlen( $name ) > 0 )
		{
										$lwhere_field = '`name` = ';
			
								
								$lwhere_field .= "'" . s( $name ) . "'";
							
						
			$lwhere_fields[] = $lwhere_field;
		}
	
	if( isset( $lwhere_fields ) )
		$list_where =  join( $where_logic , $lwhere_fields ) . " ";
	else
		$list_where = "";
	


	// 检查该记录是否存在
	// 处理返回值
		$out_field[] = '`level`';
		$out_field[] = '`password`';
		$out_field[] = '`name`';
		$out_field[] = '`email`';
		$out_field[] = '`id`';
	
	// sinceID
	$sid = intval(v('_since_id'));

	if( intval(v('_count')) > 0 ) $page_count = intval(v('_count'));
	if( $page_count > 1000 ) $page_count = 1000;
	$limit_sql = " LIMIT " . $page_count;
	
	// 排序
	$by = z(t(v('_order_by')));
	if( strlen( $by ) < 1 ) $by = 'id';

	$table_fields = array();
		$table_fields[] = 'id'; 
		$table_fields[] = 'email'; 
		$table_fields[] = 'name'; 
		$table_fields[] = 'password'; 
		$table_fields[] = 'level'; 
		if( !in_array( $by , $table_fields ) ) return send_error('INPUT' , '错误的字段名称');

	if( strtolower(t(v('_order'))) == 'asc' )
	{
		$order_sql = " ORDER BY `" . $by . "` ASC ";
		
		if( $sid > 0 )
			$where_fields[] = " `id` >= '" . intval( $sid ) . "' ";
	} 
	else
	{
		$order_sql = " ORDER BY `" . $by . "` DESC ";	
		if( $sid > 0 )
			$where_fields[] = " `id` <= '" . intval( $sid ) . "' ";
	}
		
	
	if( isset($where_fields) )
		$main_where = join( " AND " , $where_fields );
	else	
		$main_where = "";

	
	if( strlen( $list_where ) > 0 && strlen( $main_where ) > 0 )
		$where_sql = " WHERE " . $main_where . " AND " . $list_where ;
	elseif( strlen( $list_where ) > 0 )
		$where_sql = " WHERE " . $list_where ;
	elseif( strlen( $main_where ) > 0 )	
		$where_sql = " WHERE " . $main_where ;
	else
		$where_sql = "";			

	// 业务逻辑控制代码
	
// 在$sql拼接之前，可以对其组成数组进行修改
// 在UPDATE、DELETE操作中，可使用$last对原始记录进行检查		

	$sql = "SELECT " . join( ' , ' , $out_field ) . " FROM `user` " . $where_sql . " " . $order_sql  . " " . $limit_sql ;
	

	$return_item = get_data( $sql );

	// 输出控制代码
	
// 在return之前运行，可以对$return_item 进行修改

	if( $return_item )
	{
		foreach( $return_item as $item )
			$iid[] = $item['id'];

		$return_item['max_id'] = max( $iid );
		$return_item['min_id'] = min( $iid );

		return render_json( $return_item );	
	} 
	else
	{
		return send_error('NODATA' , '没有对应的记录，可能数据表为空');
	};
}

	    
	    	    
	}
}

